   //
//  WeiboView.m
//  Weibo
//
//  Created by taoshanchang on 15/1/29.
//  Copyright (c) 2015年 taoshanchang. All rights reserved.
//

#import "WeiboView.h"
#import "UIFaactory.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"
#import "RegexKitLite.h"
#import "NSString+URLEncoding.h"
#import "UIUtils.h"
#import "CONSTS.h"
#import "UIView+Addition.h"

@implementation WeiboView

-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if(self){
        [self _initView];
        _parsedText = [NSMutableString string];
    
    }
    return self;
    
}


-(void) _initView{
    //微博内容
    _textLabel = [[RTLabel alloc] initWithFrame:CGRectZero];
    _textLabel.delegate = self;
    _textLabel.font = [UIFont systemFontOfSize:14.0f];
    _textLabel.linkAttributes = [NSDictionary dictionaryWithObject:@"#4595CB" forKey:@"color"];
    _textLabel.selectedLinkAttributes = [NSDictionary dictionaryWithObject:@"darkGray" forKey:@"color"];
    [self addSubview:_textLabel];
    //微博图片
    _image = [[UIImageView alloc] initWithFrame:CGRectZero];
    _image.image = [UIImage imageNamed:@"page_image_loading"];
    [self addSubview:_image];
    
    _repostBackgroundView = [UIFaactory createImageView:@"timeline_retweet_background"];
    UIImage  *image= [_repostBackgroundView.image stretchableImageWithLeftCapWidth:25 topCapHeight:10];//设置被拉伸的图片
    _repostBackgroundView.image = image;
    _repostBackgroundView.topCapHeight = 10;
    _repostBackgroundView.leftCapWidth = 25;
    _repostBackgroundView.backgroundColor = [UIColor clearColor];
    [self insertSubview:_repostBackgroundView atIndex:0];
}

-(void)setWeiboModel:(WeiboModel *)weiboModel{
    if(_weiboModel != weiboModel){
        _weiboModel = weiboModel;
    }
    //转发的微博视图
    if(_repostView ==  nil){
        _repostView = [[WeiboView alloc] initWithFrame:CGRectZero];
        _repostView.isRespost = YES;
        _repostView.isDetail = self.isDetail;
        [self addSubview:_repostView];
    }
    
    [self parseLink];

}
//解析超链接
-(void)parseLink{
    [_parsedText setString:@""];
    
    //判断是否是转发的微博，如果是，就将源微博的作者加上，如加上  @tsc
    if(_isRespost){
        //元微博的作者昵称
        NSString *nickName = _weiboModel.user.screen_name;
        NSString *encodeName = [nickName URLEncodedString];
        [_parsedText appendFormat:@"<a href='user://%@'>@%@</a>: ",encodeName,nickName];
    }

    NSString *text = _weiboModel.text;
    text = [UIUtils parseLink:text];
    [_parsedText appendString:text];
}

//设置数据  设置子视图布局
-(void)layoutSubviews{
    [super layoutSubviews];
    //------------------微博内容
    [self _renderLabel];
    //---------------源微博视图
    [self _renderSourceWeiboView];
    //---------------微博图片视图
    [self _renderImage];
    //---------------转发微博的是背景
    if (self.isRespost) {
        _repostBackgroundView.frame = self.bounds;
        _repostBackgroundView.hidden = NO;
    }else{
        _repostBackgroundView.hidden = YES;
    }
    
    
    
}
//渲染微博内容
-(void)_renderLabel{
    float fontSize = [WeiboView getFontSize:self.isDetail isRepost:self.isRespost];
    _textLabel.font = [UIFont systemFontOfSize:fontSize];
    _textLabel.frame = CGRectMake(0, 0, self.width, 20);
    
    if (self.isRespost) {
        _textLabel.frame = CGRectMake(10, 10, self.width-20, 20);
    }
    _textLabel.text = _parsedText;
    //文本的内容的尺寸
    CGSize textSize = _textLabel.optimumSize;
    _textLabel.height = textSize.height;
}
//渲染微博视图
-(void)_renderSourceWeiboView{
    WeiboModel *repostWeibo = _weiboModel.relWeibo;
    if(repostWeibo != nil){
        _repostView.weiboModel = repostWeibo;
        float height = [WeiboView getWeiboViewHeight:repostWeibo isRepost:YES isDetail:self.isDetail];
        _repostView.frame = CGRectMake(0, _textLabel.bottom, self.width, height);
        _repostView.hidden = NO;
    }else{
        _repostView.hidden = YES;
    }
}
//渲染微博图片
-(void)_renderImage{
    if(self.isDetail){
        //中等图,不需要判断是否需要大图浏览
        NSString *bmiddleImage =  _weiboModel.bmiddleImage;
        if(bmiddleImage != nil && ![@"" isEqualToString:bmiddleImage]){
            _image.hidden = NO;
            _image.frame = CGRectMake((ScreenWidth-280)/2, _textLabel.bottom+10, 280, 200);
            
            [_image sd_setImageWithURL:[NSURL URLWithString:bmiddleImage]];
        }else{
            _image.hidden = YES;
        }
    }else{
        //判断是是否启用了大图浏览
        long model = [[NSUserDefaults standardUserDefaults] integerForKey:kBrowModel];
        //缩略图
        if(model == SmallBrowModel || model == 0){
            NSString *thumbnailImage =  _weiboModel.thumbnailImage;
            if(thumbnailImage != nil && ![@"" isEqualToString:thumbnailImage]){
                _image.hidden = NO;
                _image.frame = CGRectMake(10, _textLabel.bottom+10, 70, 80);
                
                [_image sd_setImageWithURL:[NSURL URLWithString:thumbnailImage]];
            }else{
                _image.hidden = YES;
            }
        }
        //大图
        else if(model == LargBrowModel){
            NSString *bmiddleImage =  _weiboModel.bmiddleImage;
            if(bmiddleImage != nil && ![@"" isEqualToString:bmiddleImage]){
                _image.hidden = NO;
                _image.frame = CGRectMake((self.width-(self.width-20))/2, _textLabel.bottom+10, self.width-20, 180);
                
                [_image sd_setImageWithURL:[NSURL URLWithString:bmiddleImage]];
            }else{
                _image.hidden = YES;
            }
        }
        
    }
}

+(CGFloat )getWeiboViewHeight:(WeiboModel *)weiboModel isRepost:(BOOL)isRepost isDetail:(BOOL)isDetail{
    float height = 0.0;
    //----------------------计算微博内容的高度------------------
    RTLabel *textLabel = [[RTLabel alloc] initWithFrame:CGRectZero];
    float fontSize = [WeiboView getFontSize:isDetail isRepost:isRepost];
    textLabel.font = [UIFont systemFontOfSize:fontSize];
    
    if(isDetail){
        textLabel.width = kWeibo_Detail_width;
    }else{
        textLabel.width = kWeibo_List_width;
    }
    
    NSString *weiboText = nil;
    if(isRepost){
        textLabel.width -= 20;
        NSString *nickName = weiboModel.user.screen_name;
        weiboText = [NSString stringWithFormat:@"@%@:%@",nickName,weiboModel.text];
    }else{
        weiboText =weiboModel.text;
    }
    
    textLabel.text = weiboText;
    //加上微博内容的高度
    height += textLabel.optimumSize.height;
    
    //-----------------计算微博图片的搞度-----------
    if(isDetail){
        NSString *bmiddleImage= weiboModel.bmiddleImage;
        if(bmiddleImage != nil && ![@"" isEqualToString:bmiddleImage]){
            height += (200 + 10 );
        }
    }else{
        //判断是是否启用了大图浏览
        long model = [[NSUserDefaults standardUserDefaults] integerForKey:kBrowModel];
        if(model == SmallBrowModel || model == 0){
            NSString *thumbnailImage= weiboModel.thumbnailImage;
            if(thumbnailImage != nil && ![@"" isEqualToString:thumbnailImage]){
                height += (80 + 10 );
            }
        }else if(model == LargBrowModel){
            NSString *bmiddleImage= weiboModel.bmiddleImage;
            if(bmiddleImage != nil && ![@"" isEqualToString:bmiddleImage]){
                height += (180 + 10 );
            }
        }
       
    }
    
    
    //-----------转发的微博view 的高度----------
    WeiboModel *relWeibo = weiboModel.relWeibo;
    if(relWeibo != nil){
        float repostHeight= [WeiboView getWeiboViewHeight:relWeibo isRepost:YES isDetail:isDetail];
        height += repostHeight;
    }
    //暂时这样处理
    if(isRepost){
        height += 20;
    }
    
    return height;

}

+(float)getFontSize:(BOOL)isDetail isRepost:(bool)isRepost{
    float fontSize = 14.0f;
    if (!isDetail && !isRepost) {
        return LIST_FONT;
    }else if(!isDetail && isRepost){
        return LIST_REPOST_FONT;
    }else if(isDetail && !isRepost){
        return DETAIL_FONT;
    }else if(isRepost && isDetail){
        return DETAIL_REPOST_FONT;
    }
    return fontSize;
}


#pragma - mark RTLabel delegate
- (void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSURL*)url{
    
    NSString *absoultString = [url absoluteString];
    if([absoultString hasPrefix:@"user"]){
        NSString *urlString = [url host];
        urlString = [urlString URLDecodedString];
        [self.viewController.navigationController pushViewController:<#(UIViewController *)#> animated:YES];
    }else if([absoultString hasPrefix:@"http"]){
        NSLog(@"连接：%@",absoultString);
    }else if([absoultString hasPrefix:@"topic"]){
        NSString *urlString = [url host];
        urlString = [urlString URLDecodedString];
        NSLog(@"话题：%@",urlString);
    }
    
}


@end
